package com.winxuan.channel.controller.commodity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.winxuan.channel.controller.BaseController;
import com.winxuan.channel.model.commodity.CommoditySnap;
import com.winxuan.channel.model.commodity.CommoditySnapPK;
import com.winxuan.channel.model.object.ChannelResult;
import com.winxuan.channel.service.commodity.SnapService;
import com.winxuan.channel.support.interceptor.PaginationAware;
import com.winxuan.util.pagination.Pagination;

@Controller
@RequestMapping(value="/commoditysnap")
public class CommoditySnapController extends BaseController implements PaginationAware{

	@Autowired
	private SnapService snapService;

	@RequestMapping(value="/list",method=RequestMethod.POST)
	public ChannelResult getList(@RequestParam("distribute")Integer distribute,
			@RequestParam("commodity")Integer commodity,
			@RequestParam("limit")Integer limit,
			@RequestParam("page")Integer page){
		ChannelResult channelResult = new ChannelResult();
		pagination.setPageSize(limit);
		pagination.setCurrentPage(page);
		List<CommoditySnap> commoditySnaps =
			snapService.getCommoditySnapByDistribute(distribute, commodity, pagination);
		channelResult.setSuccess(true);
		channelResult.setTotalCount(pagination.getCount());
		channelResult.setResult(commoditySnaps);
		return channelResult;
	}

	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ChannelResult add(Integer distribute,
			Integer commodity,String distributeCommodity,
			String name,BigDecimal price,boolean onshelf,Integer stock,
			String coversdigest,String descriptiondigest){
		ChannelResult channelResult = new ChannelResult();
		CommoditySnap commoditySnap = new CommoditySnap();
		CommoditySnapPK snapPK = new CommoditySnapPK();
		snapPK.setDistribute(distribute);
		snapPK.setCommodity(commodity);
		commoditySnap.setCommoditySnapPK(snapPK);
		commoditySnap.setDistributeCommodity(distributeCommodity);
		commoditySnap.setName(name);
		commoditySnap.setPrice(price);
		commoditySnap.setOnshelf(onshelf);
		commoditySnap.setStock(stock);
		commoditySnap.setCoversdigest(coversdigest);
		commoditySnap.setDescriptiondigest(descriptiondigest);
		commoditySnap.setCreatetime(new Date());
		commoditySnap.setLastupdatetime(new Date());
		snapService.addCommoditySnap(commoditySnap);
		channelResult.setSuccess(true);
		return channelResult;
	}

	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public ChannelResult edit(Integer distribute,
			Integer commodity,String distributeCommodity,
			String name,BigDecimal price,boolean onshelf,Integer stock,
			String coversdigest,String descriptiondigest){
		ChannelResult channelResult = new ChannelResult();
		CommoditySnap commoditySnap = snapService.getCommoditySnap(distribute, commodity);
		if(commoditySnap!=null){
			CommoditySnapPK snapPK = new CommoditySnapPK();
			snapPK.setDistribute(distribute);
			snapPK.setCommodity(commodity);
			commoditySnap.setCommoditySnapPK(snapPK);
			commoditySnap.setDistributeCommodity(distributeCommodity);
			commoditySnap.setName(name);
			commoditySnap.setPrice(price);
			commoditySnap.setOnshelf(onshelf);
			commoditySnap.setStock(stock);
			commoditySnap.setCoversdigest(coversdigest);
			commoditySnap.setDescriptiondigest(descriptiondigest);
			commoditySnap.setCreatetime(new Date());
			commoditySnap.setLastupdatetime(new Date());
			snapService.updateCommoditySnap(commoditySnap);
			channelResult.setSuccess(true);
		}else{
			channelResult.setSuccess(false);
			channelResult.setMsg("没有找到此快照!");
		}
		return channelResult;
	}
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public ChannelResult del(String distributes,String commodities){
		ChannelResult channelResult = new ChannelResult();
		if(distributes !=null && commodities != null){
			String[] distribute = distributes.split(",");
			String[] commodity = commodities.split(",");
			if(distribute.length == commodity.length){
				for(int i=0;i<commodity.length;i++){
					Integer dis = Integer.valueOf(distribute[i]);
					Integer com = Integer.valueOf(commodity[i]);
					CommoditySnap commoditySnap = snapService.getCommoditySnap(dis, com);
					if(commoditySnap!=null){
						snapService.delCommoditySnap(commoditySnap);
					}
				}
				channelResult.setSuccess(true);
			}else{
				channelResult.setSuccess(false);
			}
		}
		return channelResult;
	}
	
	@RequestMapping(value="/search",method=RequestMethod.POST)
	public ChannelResult search(Integer distribute,Integer commodity,
			String distributeCommodity,String name,
			String coversdigest){
		ChannelResult channelResult = new ChannelResult();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("distribute", distribute);
		params.put("commodity", commodity);
		params.put("distributeCommodity", distributeCommodity);
		params.put("name", name);
		params.put("coversdigest", coversdigest);
		List<CommoditySnap> commoditySnaps = 
			snapService.searchByMap(params, pagination);
		if(commoditySnaps != null && commoditySnaps.size()>0){
			channelResult.setSuccess(true);
			channelResult.setResult(commoditySnaps);
			channelResult.setTotalCount(pagination.getCount());
		}else{
			channelResult.setSuccess(false);
			channelResult.setMsg("没有找到记录!");
		}
		return channelResult;
	}

	@Override
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
}
